1753B - Factorial Divisibility - CodeForces Solution


greedy math number theory *1600

Please click on ads to support us..

Python Code:

import sys, array, bisect, collections, heapq, itertools, math
sys.setrecursionlimit(100000)

def _r(): return sys.stdin.buffer.readline()
def rs(): return _r().decode('ascii').strip()
def rn(): return int(_r())
def rnt(): return tuple(map(int, _r().split()))
def rnl(): return list(rnt())
def rna(): return array.array('q', rnt())
def pnl(l): print(' '.join(map(str, l)))

def solve(n, x, a):
    cnts = [0]*x
    for num in a:
        cnts[num-1] += 1
    for i in range(x-1):
        while cnts[i] >= i+2:
            cnts[i] -= i+2
            cnts[i+1] += 1
    return sum(cnts[i] for i in range(x-1)) == 0

n, x = rnt()
a = rnl()
print('YES' if solve(n, x, a) else 'NO')


C++ Code:

// - kylezft -

#include <bits/stdc++.h>

using namespace std;

#define task_ "task"
#define fst() ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define fin() freopen(task_".inp", "r", stdin)
#define fout() freopen(task_".out", "w", stdout)
#define ll long long
#define inf 2e9
#define oo 2e18
#define sz(s) int(s.size())
#define fi first
#define se second
#define bit(k, n) ((n >> k) & 1)
#define cntbit(n) __builtin_popcount(n)

mt19937 rd(chrono::high_resolution_clock::now().time_since_epoch().count());

// - <\> -

const int N = 5e5 + 2;

int n, a[N], x, cnt[N];

int main() {
    fst();

    cin >> n >> x;
    for (int i = 1; i <= n; ++i) {
    	cin >> a[i];
    	++cnt[a[i]];
    }
    bool ok = true;
    for (int i = 1; i < x; ++i) {
    	if (cnt[i] >= i + 1) {
    		cnt[i + 1] += cnt[i] / (i + 1);
    		cnt[i] %= i + 1;
    	}
    	if (cnt[i] != 0) ok = false;
    }
    cout << (ok ? "Yes" : "No");
}


Comments

Submit
0 Comments
More Questions

1547A - Shortest Path with Obstacle
624A - Save Luke
1238A - Prime Subtraction
1107C - Brutality
1391B - Fix You
988B - Substrings Sort
312A - Whose sentence is it
513A - Game
1711E - XOR Triangle
688A - Opponents
20C - Dijkstra
1627D - Not Adding
893B - Beautiful Divisors
864B - Polycarp and Letters
1088A - Ehab and another construction problem
1177B - Digits Sequence (Hard Edition)
1155B - Game with Telephone Numbers
1284A - New Year and Naming
863B - Kayaking
1395B - Boboniu Plays Chess
1475D - Cleaning the Phone
617B - Chocolate
1051B - Relatively Prime Pairs
95B - Lucky Numbers
1692D - The Clock
1553D - Backspace
1670D - Very Suspicious
1141B - Maximal Continuous Rest
1341A - Nastya and Rice
1133A - Middle of the Contest